package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils2.string.IceStringUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.dao.OrderCreditDao;
import com.edt.ichannel.api.entiy.local.OrderCredit;
import com.edt.ichannel.api.service.OrderCreditService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderCreditServiceImpl implements OrderCreditService {
    private Logger logger = LogManager.getLogger(OrderCreditServiceImpl.class);

    @Resource
    private OrderCreditDao orderCreditDao;
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveOrderCreditList(List<OrderCredit> orderCredits) {
        if (orderCredits != null){
            int count = CommonConstant.BRANCH_COUNT;
            int lastIndex = count ;
            for(int index = 0; index < orderCredits.size();){
                if(lastIndex > orderCredits.size()){
                    lastIndex = orderCredits.size();
                    orderCreditDao.saveList(orderCredits.subList(index,lastIndex));
                    break;
                }else{
                    orderCreditDao.saveList(orderCredits.subList(index, lastIndex));
                    index = lastIndex;
                    lastIndex = index + (count);
                }
            }
            logger.info("list 长度："+orderCredits.size());
        }else{
            logger.info("list为空");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void upadateOrderCteditMoney(OrderCredit orderCredit) {
        orderCreditDao.upadateOrderCteditMoney(orderCredit);
    }

    @Override
    public List<OrderCredit> selectByBatch(String batchCode) {
        return orderCreditDao.selectByBatch(batchCode);
    }

    @Override
    public OrderCredit selectByOrderCode(String orderCode) {
        if(IceStringUtils.isBlank(orderCode)){
            return null;
        }
        return orderCreditDao.selectByOrderCode(orderCode);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateFirstDateList(List<OrderCredit> orderCredits) {
        if (orderCredits != null){
            int count = CommonConstant.BRANCH_COUNT;
            int lastIndex = count ;
            int successCount = 0;
            for(int index = 0; index < orderCredits.size();){
                if(lastIndex > orderCredits.size()){
                    lastIndex = orderCredits.size();
                    successCount+=orderCredits.size()-index;
                    orderCreditDao.updateFirstDateList(orderCredits.subList(index,lastIndex));
                    break;
                }else{
                    orderCreditDao.updateFirstDateList(orderCredits.subList(index, lastIndex));
                    successCount+=orderCredits.size()-index;
                    index = lastIndex;
                    lastIndex = index + (count);
                }
            }
            logger.info("list 长度："+orderCredits.size());
            logger.info("成功"+successCount);
        }else{
            logger.info("list为空");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void upadateOrderCteditCommissionMoney(OrderCredit orderCredit) {
        orderCreditDao.upadateOrderCteditCommissionMoney(orderCredit);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void upadateOrderCteditBonusMoney(OrderCredit orderCredit) {
        orderCreditDao.upadateOrderCteditBonusMoney(orderCredit);
    }

    @Override
    public List<OrderCredit> selectNeedComputCommission(String batchCode) {
        return orderCreditDao.selectNeedComputCommission(batchCode);
    }

    @Override
    public List<OrderCredit> selectNeedComputBonus(String batchCode) {
        return orderCreditDao.selectNeedComputBonus(batchCode);
    }

    @Override
    public void addCommissionMoney(String orderCode, Double commissionMoney) {
        orderCreditDao.addCommissionMoney(orderCode,commissionMoney);
    }

}
